﻿using NoteHelper.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NoteHelper.Sys
{
	public partial class Sysm002 : WeifenLuo.WinFormsUI.Docking.DockContent,IBaseView
	{
		private const string Pre = "Data_";
		private log4net.ILog logger;
		private DevAge.ComponentModel.IBoundList bd;
		public Sysm002()
		{
			InitializeComponent();
		}

		private void Sysm002_Load(object sender, EventArgs e)
		{
			logger = log4net.LogManager.GetLogger("NoteHelper.Sys.Sysm002");
			displayGrid.FixedRows = 1;
			displayGrid.FixedColumns = 1;
			CreateColumns();
			var controller = new Sysm002Controller(this);
		}
		private void CreateColumns()
		{
			SourceGrid.DataGridColumn gridColumn;
			gridColumn = displayGrid.Columns.Add("$_rowIndex", "下标", typeof(string));
			gridColumn.Width = 100;
			gridColumn = displayGrid.Columns.Add("ObjectID", "编号", typeof(string));
			gridColumn.Width = 100;
		}
		#region ==== IBaseView Impl ====

		private DataTable _displayList;
		public DataTable DisplayList
		{
			get { return _displayList; }
			set
			{
				if (value == null) return;
				_displayList = value;
				
				bd = new DevAge.ComponentModel.BoundDataView(_displayList.DefaultView);
				bd.AllowNew = false;
				bd.AllowEdit = false;
				bd.AllowDelete = false;
				displayGrid.DataSource = bd;
			}
		}
		public FormCommand Cmd { get; set; }
		public Hashtable RowData
		{
			get
			{
				return splitContainer1.Panel1.GetData(Pre);
			}
			set
			{
				splitContainer1.Panel1.SetData(Pre, value);
			}
		}
		public event EventHandler Insert;
		public event EventHandler Edit;
		public event EventHandler Remove;
		public event EventHandler Copy;
		public event EventHandler Query;
		public event EventHandler Accept;
		public event EventHandler Cancel;
		public event EventHandler Print;
		public event EventHandler Help;
		public void BeginEdit()
		{
			splitContainer1.Panel1.ChangeInputState(Pre, true);
		}
		public void EndEdit()
		{
			splitContainer1.Panel1.ChangeInputState(Pre, false);
		}
		public int GetRowIndex()
		{
			int rowIndex = -1;
			object[] rows = displayGrid.SelectedDataRows;
			if (rows != null && rows.Length > 0)
			{
				DataRowView row = (DataRowView)rows[0];
				int.TryParse(row["$_rowIndex"].ToString(), out rowIndex);
			}
			return rowIndex;
		}

		public void Notify(string notification)
		{
			AlterMsg.Info(notification);
		}
		#endregion

		#region ==== Toolbar Event ====
		/// <summary>
		/// 添加
		/// </summary>
		private void InsertButtonClick(object sender, EventArgs e)
		{
			Insert?.Invoke(sender, e);
		}
		/// <summary>
		/// 修改
		/// </summary>
		private void EditButtonClick(object sender, EventArgs e)
		{
			Edit?.Invoke(sender, e);
		}
		/// <summary>
		/// 删除
		/// </summary>
		private void RemoveButtonClick(object sender, EventArgs e)
		{
			Remove?.Invoke(sender, e);
		}
		/// <summary>
		/// 复制
		/// </summary>
		private void CopyButtonClick(object sender, EventArgs e)
		{
			Copy?.Invoke(sender, e);
		}
		/// <summary>
		/// 查询
		/// </summary>
		private void QueryButtonClick(object sender, EventArgs e)
		{
			Query?.Invoke(sender, e);
		}
		/// <summary>
		/// 确定
		/// </summary>
		private void AcceptButtonClick(object sender, EventArgs e)
		{
			Accept?.Invoke(sender, e);
		}
		/// <summary>
		/// 取消
		/// </summary>
		private void CancelButtonClick(object sender, EventArgs e)
		{
			Cancel?.Invoke(sender, e);
		}
		/// <summary>
		/// 打印
		/// </summary>
		private void PrintButtonClick(object sender, EventArgs e)
		{
			Print?.Invoke(sender, e);
		}
		/// <summary>
		/// 帮助
		/// </summary>
		private void HelpButtonClick(object sender, EventArgs e)
		{
			Help?.Invoke(sender, e);
		}
		#endregion
	}
}
